import os
import profile

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver import FirefoxProfile
from pprint import pprint

# 打开浏览器
driver = webdriver.Chrome()

# TODO 单选/多选按钮
'''
# 打开测试网址
url = 'file:///' + os.path.abspath('html') + '/radio-checkbox.html'
driver.get(url)
# 单选按钮操作
# time.sleep(3)
f1 = driver.find_element(By.ID, 'boy')
f1.click()
print(f1.is_selected()) # is_selected() 判断是否被选中
time.sleep(3)
f2 = driver.find_element(By.ID, 'girl')
f2.click()
print(f2.is_selected())

# 多选按钮
# 第一种标签定位
f1 = driver.find_elements(By.XPATH, '//input[@type="checkbox"]')
for i in f1:
    time.sleep(3)
    i.click()

# 第二种标签定位
f2 = driver.find_elements(By.TAG_NAME, 'form')[1]
ipt = f2.find_elements(By.TAG_NAME, 'input')
for i in ipt:
    time.sleep(3)
    i.click()
'''
# # 打开163邮箱
# # driver.get('https://mail.163.com')
# # 登录163邮箱，退出浏览器
# # 准备配置
# # C:\Users\zhang\AppData\Local\Google\Chrome\User Data\Default
# options = webdriver.ChromeOptions()
# user_data_dir = r'--user-data-dir=C:\Users\zhang\AppData\Local\Google\Chrome\User Data\Default'
# options.add_argument(user_data_dir)
#
# # 打开浏览器（设置options），请求目标网址
# driver = webdriver.Chrome(options=options)
# driver.get('https://mail.163.com')

# C:\Users\zhang\AppData\Roaming\Mozilla\Firefox\Profiles\oacjca7i.default - release
# 准备配置
# profile_directory = r'C:\Users\zhang\AppData\Roaming\Mozilla\Firefox\Profiles\oacjca7i.default-release'
# profile1 = FirefoxProfile(profile_directory)
#
# # 打开浏览器
# driver = webdriver.Firefox(profile1)
# driver.get('https://mail.163.com')

# TODO 富文本操作，上传及下载
'''
# 文件上传
# 打开测试网址
url = 'file:///' + os.path.abspath('html') + '/upload_file.html'
driver.get(url)
# 上传文件路径, 路径为绝对路径
# os.path.abspath(__file__)当前脚本所在相对路径，os.path.dirname()目录绝对路径
file = os.path.dirname(os.path.abspath(__file__)) + '/geckodriver.log'
# 定位
driver.find_element(By.NAME, 'file').send_keys(file)

# 下载文件
# 准备配置项
options = webdriver.ChromeOptions()
# 准备下载路径，可以下载在任何地方，路径为保存下载的绝对路径
download = os.path.dirname(os.path.abspath(__file__)) + '/download'
prefs ={
    'profile.default_content_settings.popups':0,
    'download.default_directory':download
}
options.add_experimental_option('prefs', prefs)
# 打开浏览器，设置options，请求网址
driver = webdriver.Chrome(options=options)
driver.get('https://www.selenium.dev/downloads/')
# 定位要下载文件
driver.find_element(By.LINK_TEXT, '64 bit Windows IE').click()
'''
# todo cookies 操作

# 打开网址
driver.get('https://www.baidu.com')
# 获取所有cookies信息并打印
pprint(driver.get_cookies())
# get_cookie(name) 获得特定name值的cookie信息
pprint(driver.get_cookie('BA_HECTOR'))
# add_cookie(cookie_dict)添加cookie，必须有name 和value
cookie_dict = {'name':'abc', 'value':'123'}
driver.add_cookie(cookie_dict)
pprint(driver.get_cookies())
# delete_cookie(name)删除特定（部分）的cookie信息
driver.delete_cookie('abc')
pprint(driver.get_cookies())
# delete_all_cookies()删除所有cookie信息
driver.delete_all_cookies()
pprint(driver.get_cookies())

# todo 利用cookie登录
# cookies = [
#                 {
#                 'name': 'BDUSS'
#                 'value': 'hEdWRrTEM0bTNrOGo4NTlvUmR5d3Y3U3U4enZwUVZTUlJPNGU1QkdGNFptcWxpRVFBQUFBJCQAAAAAAAAAAAEAAAAIo8Qt1vHO97zRtKa94rCwydkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABkNgmIZDYJiYn'
#             },
#             {
#                'name':'BDUSS_BFESS',
#                'value':'hEdWRrTEM0bTNrOGo4NTlvUmR5d3Y3U3U4enZwUVZTUlJPNGU1QkdGNFptcWxpRVFBQUFBJCQAAAAAAAAAAAEAAAAIo8Qt1vHO97zRtKa94rCwydkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABkNgmIZDYJiYn'
#             }
#         ]
cookies = [
    {
        'name':'BDUSS_BFESS',
        'value':'p3VX4taUh0c1FRRmJ-fjJPaTFEamNxSVhkdHl3UEl2NFlZVXpxd0l5fnYtYXBpRVFBQUFBJCQAAAAAAAAAAAEAAAAIo8Qt1vHO97zRtKa94rCwydkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAO9sg2LvbINiOU'
    },
    {
        'name':'BDUSS',
        'value':'p3VX4taUh0c1FRRmJ-fjJPaTFEamNxSVhkdHl3UEl2NFlZVXpxd0l5fnYtYXBpRVFBQUFBJCQAAAAAAAAAAAEAAAAIo8Qt1vHO97zRtKa94rCwydkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAO9sg2LvbINiOU'
    }
]
# 添加cookies
driver.add_cookie(cookies[0])
driver.add_cookie(cookies[1])
# 刷新浏览器
driver.refresh()

# 退出浏览器
time.sleep(3)
driver.quit()
